package com.irdstudio.efp.flow.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.efp.flow.service.dao.BizEventInfoDao;
import com.irdstudio.efp.flow.service.domain.BizEventInfo;
import com.irdstudio.efp.flow.service.facade.BizEventInfoService;
import com.irdstudio.efp.flow.service.vo.BizEventInfoVO;
import com.irdstudio.basic.framework.core.base.AbstractFrameworkService;
import com.irdstudio.basic.framework.core.base.FrameworkService;
/**
 * <p>ServiceImpl: 业务流转事件信息				<p>
 * @author ligm
 * @date 2018-05-05
 */
@Service("bizEventInfoService")
public class BizEventInfoServiceImpl extends AbstractFrameworkService implements BizEventInfoService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(BizEventInfoServiceImpl.class);

	@Autowired
	private BizEventInfoDao bizEventInfoDao;
	
	/**
	 * 新增操作
	 */
	public int insertBizEventInfo(BizEventInfoVO inBizEventInfoVo) {
		logger.debug("当前新增数据为:"+ inBizEventInfoVo.toString());
		int num = 0;
		try {
			BizEventInfo bizEventInfo = new BizEventInfo();
			beanCopy(inBizEventInfoVo, bizEventInfo);
//			bizEventInfo.setBizEventId(this.getSequence(SequenceConstance.BIZ_EVENT_SEQ_ID));
			num = bizEventInfoDao.insertBizEventInfo(bizEventInfo);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(BizEventInfoVO inBizEventInfoVo) {
		logger.debug("当前删除数据条件为:"+ inBizEventInfoVo);
		int num = 0;
		try {
			BizEventInfo bizEventInfo = new BizEventInfo();
			beanCopy(inBizEventInfoVo, bizEventInfo);
			num = bizEventInfoDao.deleteByPk(bizEventInfo);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBizEventInfoVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(BizEventInfoVO inBizEventInfoVo) {
		logger.debug("当前修改数据为:"+ inBizEventInfoVo.toString());
		int num = 0;
		try {
			BizEventInfo bizEventInfo = new BizEventInfo();
			beanCopy(inBizEventInfoVo, bizEventInfo);
			num = bizEventInfoDao.updateByPk(bizEventInfo);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBizEventInfoVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public BizEventInfoVO queryByPk(BizEventInfoVO inBizEventInfoVo) {
		
		logger.debug("当前查询参数信息为:"+ inBizEventInfoVo);
		try {
			BizEventInfo queryBizEventInfo = new BizEventInfo();
			beanCopy(inBizEventInfoVo, queryBizEventInfo);
			BizEventInfo queryRslBizEventInfo = bizEventInfoDao.queryByPk(queryBizEventInfo);
			if (Objects.nonNull(queryRslBizEventInfo)) {
				BizEventInfoVO outBizEventInfoVo = beanCopy(queryRslBizEventInfo, new BizEventInfoVO());
				logger.debug("当前查询结果为:"+ outBizEventInfoVo.toString());
				return outBizEventInfoVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizEventInfoVO> queryAllOwner(BizEventInfoVO bizEventInfoVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<BizEventInfoVO> list = null;
		try {
			List<BizEventInfo> bizEventInfos = bizEventInfoDao.queryAllOwnerByPage(bizEventInfoVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ bizEventInfos.size());
			pageSet(bizEventInfos, bizEventInfoVo);
			list = (List<BizEventInfoVO>) beansCopy(bizEventInfos, BizEventInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizEventInfoVO> queryAllCurrOrg(BizEventInfoVO bizEventInfoVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<BizEventInfo> bizEventInfos = bizEventInfoDao.queryAllCurrOrgByPage(bizEventInfoVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+bizEventInfos.size());
		List<BizEventInfoVO> list = null;
		try {
			pageSet(bizEventInfos, bizEventInfoVo);
			list = (List<BizEventInfoVO>) beansCopy(bizEventInfos, BizEventInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizEventInfoVO> queryAllCurrDownOrg(BizEventInfoVO bizEventInfoVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<BizEventInfo> bizEventInfos = bizEventInfoDao.queryAllCurrDownOrgByPage(bizEventInfoVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ bizEventInfos.size());
		List<BizEventInfoVO> list = null;
		try {
			pageSet(bizEventInfos, bizEventInfoVo);
			list = (List<BizEventInfoVO>) beansCopy(bizEventInfos, BizEventInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}
	
	public int countByFlowIdAndEventId(String flowId, String eventId) {
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("flowId", flowId);
		paramMap.put("eventId", eventId);
		int num = 0;
		try {
			logger.info("根据流程ID:"+ flowId +",事件ID:"+ eventId +"查询开始.....");
			num = this.bizEventInfoDao.countByFlowIdAndEventId(paramMap);
			logger.info("根据流程ID:"+ flowId +",事件ID:"+ eventId +"查询结束.....");
		} catch (Exception e) {
			logger.error("根据流程ID:"+ flowId +",事件ID:"+ eventId +"查询出现异常!", e);
			num = -1;
		}
		return num;
		
	}
	
	/**
	 * 根据业务流转类型查找事件信息
	 */
	@SuppressWarnings("unchecked")
	public List<BizEventInfoVO> queryAllByFlowType(BizEventInfoVO bizEventInfoVo) {
		List<BizEventInfoVO> list = null;
		try {
			BizEventInfo eventInfo = new BizEventInfo();
			beanCopy(bizEventInfoVo, eventInfo);
			List<BizEventInfo> bizEventInfos = bizEventInfoDao.queryAllByFlowType(eventInfo);
			list = (List<BizEventInfoVO>) beansCopy(bizEventInfos, BizEventInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	}
}
